package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import model.Database;
import controller.Engine;

@SuppressWarnings("serial")
public class Home extends JFrame{

	public static JPanel conteiner = new JPanel();
	public static JPanel profile;
	public static JPanel contact;
	public static JPanel calendar;
	public static JPanel event;
	public static JMenuBar adminMenu;
	static JFrame home = new JFrame();
	public Home(){
		home=this;
		home.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				Engine.closeDB();
				System.exit(0);
			}
		});
		startHome();
	}

	public void startHome() {

		JPanel firstColumn = new JPanel();
		firstColumn.setBackground(Color.white);
		JPanel secondColumn = new JPanel();
		secondColumn.setBackground(Color.white);
		if (Database.Utente.getLevel()==512){
			adminMenu = new Menu();
			home.setJMenuBar(adminMenu);
		}
		profile = new HomeProfile();
		contact = new HomeContact();
		calendar = new HomeCalendar();
		event = new HomeEvent();

		firstColumn.setLayout(new BoxLayout(firstColumn,BoxLayout.Y_AXIS));
		firstColumn.setAlignmentX(LEFT_ALIGNMENT);
		firstColumn.add(Box.createVerticalStrut(8));
		firstColumn.add(profile);
		//firstColumn.add(Box.createVerticalGlue());
		//firstColumn.add(Box.createVerticalStrut(7));
		firstColumn.add(contact);

		secondColumn.setLayout(new BoxLayout(secondColumn,BoxLayout.Y_AXIS));
		secondColumn.setAlignmentX(LEFT_ALIGNMENT);
		secondColumn.add(Box.createVerticalStrut(8));
		secondColumn.add(calendar);
		secondColumn.add(Box.createVerticalStrut(8));
		//secondColumn.add(Box.createVerticalGlue());
		secondColumn.add(event);


		conteiner.setLayout(new BoxLayout(conteiner,BoxLayout.X_AXIS));
		conteiner.setBackground(Color.white);
		//conteiner.add(Box.createHorizontalGlue());
		conteiner.add(firstColumn);
		//conteiner.add(Box.createHorizontalGlue());
		conteiner.add(secondColumn);
		conteiner.add(Box.createHorizontalGlue());


		home.add(conteiner);


		//posizionatore del home frame
		int a;
		if (Database.Utente.getLevel()==512)
			a=620;
		else a=600;
		home.setTitle("LoAD");
		home.setIconImage(Engine.immaginelog);
		home.setResizable(false);
		home.setVisible(true);
		home.setPreferredSize(new Dimension (1024,a));
		home.pack();
		Dimension schermo = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (schermo.width-1024)/2;
		int y = (schermo.height-a)/2;
		home.setLocation(x,y);
		home.requestFocus();

	}
	public static void pauseHome() {
		home.setEnabled(false);
	}

	public static void restart() {
		home.setVisible(true);
		home.setEnabled(true);
	}

	public static void stopHome() {
		conteiner.removeAll();
		home.remove(conteiner);
		home.dispose();
	}
}
